package com.huishou.lajiMain.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/*
 * @Author:大力王
 * @Date:2024/5/10-10-18:52
 * 开始了
 */
@Configuration
public class CorsConfig {
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true); // 允许携带cookie
        config.addAllowedOrigin("*"); // 允许任何来源
        config.addAllowedHeader("*"); // 允许任何头部
        config.addAllowedMethod("post"); // *代表允许任何方法 (GET, POST, PUT, DELETE, etc)
        config.addAllowedMethod("get");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config); // 应用到所有路径

        return new CorsFilter(source);
    }
}
